//
//  PurumMinigameBullet.m
//  PurumClothes
//
//  Created by 솔이 박 on 11. 4. 30..
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "PurumMinigameBullet.h"


@implementation PurumMinigameBullet

-(void) tick:(NSTimer *)theTimer
{
	
	[self update];	
}

-(void)update
{
	switch(state)
	{
		case BulletStateType_Shot:
		{
			[self move];
		}break;
			
		case BulletStateType_Appear:
		case BulletStateType_Explode:
		{
			[self nextState];
		}break;
			
		case BulletStateType_Disappear:
		{
			[self removeBulletImage];
		}break;
	}
}

-(void)move
{
	bulletImage.center = CGPointMake(bulletImage.center.x+velocity, bulletImage.center.y);
	
	if(bulletImage.center.x > 300)
	{
		state = BulletStateType_Disappear;
	}
}

-(void)nextState
{
	if(state == BulletStateType_Disappear)
		return;
	
	state = (BulletStateType)(state+1);
}

-(void)initialize : (UIView*)view ofBulletType: (BulletType)bulletType withStartX: (int)startX andStartY: (int)startY
{
	x = startX;
	y = startY;
	
	velocity = 10;
	
	type = bulletType;
	
	state = BulletStateType_Appear;
	
	[self buildBullet:view];
}

-(void)finalize
{
	[super finalize];
}

-(bool) isEnded
{
	if( state == BulletStateType_Disappear )
	{
		return YES;
	}
	
	return NO;
}

-(NSString*) getBulletImagePath
{
	switch(type)
	{
		case BulletType_Red:
		{
			return @"red_bullet.png";
		}
			
		case BulletType_Yellow:
		{
			return @"yellow_bullet.png";
		}
			
		case BulletType_Blue:
		{
			return @"blue_bullet.png";
		}
	}
	
	return @"";
}

-(void) loadBulletImage
{
	NSString* path = [self getBulletImagePath];
	
	UIImage *image = [UIImage imageNamed:path];

	bulletImage = [[[UIImageView alloc] initWithImage:image] autorelease];
	
	CGRect frame = bulletImage.frame;
	frame.origin = CGPointMake(x, y);
	
	bulletImage.frame = frame;
}

-(void) addBulletImage :(UIView*) view
{
	[view addSubview:bulletImage];
}

-(void) removeBulletImage
{
	if(bulletImage == 0)
		return;
	
	[bulletImage removeFromSuperview];
	bulletImage = 0;
	
	[self release];
}

-(void) buildBullet : (UIView*) view
{
	[self loadBulletImage];
	[self addBulletImage:view];
	
	[self initializeTimer];
}


-(void)initializeTimer
{
	float theInterval = 1.0f/10.0f;
	[NSTimer scheduledTimerWithTimeInterval:theInterval target:self selector:@selector(tick:) userInfo:nil repeats:YES];
}

@end
